<template>
	<view>
		<view v-if="activityStatus == 0">
			<view class="imagesize">
				<image class="upgrading" src="https://oos-cn.ctyunapi.cn/front-end/static/homeSub/images/activityStart.png" mode=""></image>
			</view>
			<view class="txt">
				<view class="txt1">
					活动开始时间
					<text class="fromDate">{{ fromDate }}</text>
					敬请期待！
				</view>
			</view>
		</view>
		<view class="header" v-if="activityStatus == 1">
			<image src="https://oos-cn.ctyunapi.cn/front-end/static/homeSub/images/banner.png" class="head"></image>
			<view class="header-title">
				<view class="service-wrap">
					<view class="service-row">
						<text>老用户邀请新用户充值、消费后，老用户获得</text>
						<text class="wallet">{{ oldMemberBalance ? oldMemberBalance : 0 }}元</text>
						<text class="service" v-if="couponType">{{ couponType }}</text>
					</view>
				</view>
			</view>
			 <!-- #ifdef APP-PLUS  -->
			<button class="btn_invitation" @click="appShare('WXSceneSession')">立即邀请</button>
					<!-- #endif -->
					<!-- #ifdef MP-WEIXIN  -->
                    <button open-type="share" class="btn_invitation">立即邀请</button>
                    <!-- #endif -->
			<!-- <button class="btn_invitation">立即邀请</button> -->
			<!-- <view class="rule" bindtap="showRule">查看活动规则</view> -->
			<view class="friends">
				<view class="f_title">已邀请好友列表</view>
				<view class="f_view">
					<view class="f_li">
						<view class="li_title">当前获得奖励总数</view>
						<view class="li_num">
							<view class="num">{{ total }}</view>
							<text class="text">元</text>
						</view>
					</view>
					<view class="line"></view>
					<view class="f_li">
						<view class="li_title">共邀请人数</view>
						<view class="li_num">
							<view class="num">{{ inviteMemberSize }}</view>
							<text class="text">人</text>
						</view>
					</view>
				</view>

				<view class="friends_list">
					<view class="list_title" v-if="inviteMemberList.length">
						<text>邀请名单</text>
						<text>获得奖励</text>
					</view>
					<view class="ul">
						<block v-for="(item, index) in inviteMemberList" :key="index">
							<view class="li">
								<view>
									<view class="li_left">
										<view class="phone">{{ item.telephone }}</view>
									</view>
									<view class="reward">
										获得
										<text class="rewardPrice">{{ item.oldMemberBalance }}</text>
										元
									</view>
								</view>
							</view>
						</block>
					</view>
				</view>
			</view>
		</view>

		<view v-if="activityStatus == 2">
			<view class="imagesize">
				<image class="upgrading" src="https://oos-cn.ctyunapi.cn/front-end/static/homeSub/images/activityOver.png" mode=""></image>
			</view>
			<view class="txt">
				<view class="txt1">哎呀！来晚了，活动结束了~</view>
			</view>
		</view>
	</view>
</template>

<script>
	'use strict';
	var https_1 = require('./../../../api/https.js');
	var newapp = getApp()
	var app = getApp()
	export default {
		data() {
			return {
				inviteMemberList: [],
				oldMemberBalance: 0,
				inviteMemberSize: 0,
				memberCouponBalance: 0,
				inviterMemberId: null,
				couponType: '',
				activityStatus: null,
				fromDate: null, // 活动开始时间
				rewardBalance: 0, // 好友获得
				total: 0, // 总获得
			};
		}
		/**
		 * 生命周期函数--监听页面加载
		 */
		,
		onLoad: function() {
			this.getOldAndNewBenifit()
		},
		  /**
     * 用户点击右上角分享
     */
	 onShareAppMessage: function () {
        var memberId = uni.getStorageSync('memberId');
		const name = app.globalData.wxAppName
        return {   
            title: `帮我点一下,一起来${name}享受充电优惠吧!`,      
            path: '/homeSub/pages/registration/registration?memberId=' + memberId,
            imageUrl: 'https://oos-cn.ctyunapi.cn/front-end/static/homeSub/images/share.png'
        };
    },
		methods: {
			getOldAndNewBenifit: function() {
				const tmp = this
				https_1.getBySellerId(app.globalData.sellerId).then(function(res) {
					if (res.code == 0) {
						tmp.setData({
							enabledState: res.data.enabledState
						});
					}
				});
				https_1.getOldAndNewBenifit(app.globalData.sellerId).then(function(res) {
					if (res.code == 0) {
						const oldMemberBalance = parseInt(res.data.oldMemberBalance) / 100
						const rewardBalance = parseInt(res.data.rewardBalance) / 100
						let couponType = Array.from(new Set(res.data.couponTypeList)).join('、')
						tmp.setData({
							rewardBalance,
							oldMemberBalance,
							couponType,
							activityStatus: res.data.activityStatus,
							fromDate: res.data.fromDate
						})
						if (tmp.activityStatus == 1) {
							tmp.getInviteMemberList()
							// this.getCodeAndCheckRegister()
						}
					}
				}).catch((err) => {
					console.log(err)
				})
			},
			getInviteMemberList: function() {
				const tmp = this
				const memberId = wx.getStorageSync('memberId')
				https_1.getInviteMemberList({
					sellerId: newapp.globalData.sellerId,
					memberId:memberId
				}).then(function(res) {
					if (res.code == 0) {
						let total = 0
						let friends = res.data.inviteMemberList
						friends.forEach((element) => {
							element.telephone =
								`${element.telephone.slice(0, 3)}******${element.telephone.slice(7, 11)}`
							element.oldMemberBalance = (element.oldMemberBalance / 100).toFixed(2)
							total = Number(element.oldMemberBalance) + total
						});
						let memberCouponBalance = parseInt(res.data.memberCouponBalance) / 100
						tmp.setData({
							total,
							inviteMemberList: friends,
							inviteMemberSize: res.data.inviteMemberSize,
							memberCouponBalance
						})
					}
				}).catch((err) => {
					console.log(err)
				})
			},
			getCodeAndCheckRegister: function() {
				const openId = wx.getStorageSync('openid')
				const tel = wx.getStorageSync('tel')
				const memberId = wx.getStorageSync('memberId')
				const tmp = this
				https_1.getCodeAndCheckRegister({
						phoneNo: parseInt(tel),
						sellerId: newapp.globalData.sellerId,
						sellerNo: app.globalData.sellerNo,
						sourceType: 0,
						inviterMemberId: memberId,
						openId
					})
					.then(function(res) {
						if (res.code == 0) {
							tmp.setData({
								inviterMemberId: res.data.inviterMemberId
							})
						}
					}).catch((err) => {
						console.log(err)
					})
			},
			//分享微信好友 -- 网页
			// appShare(scene) {
			// 	let that = this
			// 	const memberId = wx.getStorageSync('memberId')
			// 	var img="https://oos-cn.ctyunapi.cn/front-end/static/homeSub/images/share.jpg"
			// 	var url1=newapp.globalData.imgUrl
			// 	const name = app.globalData.wxAppName
			// 	var shareText = `帮我点一下,一起来${name}享受充电优惠吧!`
			// 	var sellerId=newapp.globalData.sellerId
			// 	var sellerNo=newapp.globalData.sellerNo
			// 	var sourceType=0// ios0 安卓1
			// 	// var platform=uni.getSystemInfoSync().platform
			// 	//     if(platform=='ios'){
			// 	//           sourceType=0
			// 	//     }else if(platform=='android'){
			// 	//           sourceType=1
			// 	//     }

			// 	var shareUrl=`${url1}/activityPage/zhuceActive.html?memberId=${memberId}&sellerId=${sellerId}&sellerNo=${sellerNo}&sourceType=${sourceType}`
			// 	uni.share({
			// 		provider: "weixin", //分享服务提供商（即weixin|qq|sinaweibo）
			// 		scene: scene, //场景，可取值参考下面说明。
			// 		type: 0, //分享形式
			// 		href: shareUrl, //跳转链接
			// 		title: shareText, //分享内容的标题
			// 		// summary: '', //分享内容的摘要
			// 		imageUrl: img, //图片地址
			// 		success: function(res) {
			// 			console.log("分享成功",res)
			// 		},
			// 		fail: function(err) {
			// 			uni.showToast({
			// 				title: '分享失败',
			// 				icon: 'none',
			// 				duration: 2000
			// 			})
			// 		}
			// 	});
			// },
			
			//分享微信好友--小程序
			appShare() {
				let that = this
				const memberId = wx.getStorageSync('memberId')
				var img="https://oos-cn.ctyunapi.cn/front-end/static/homeSub/images/share.jpg"
				const name = app.globalData.wxAppName
				var shareText = `帮我点一下,一起来${name}享受充电优惠吧!`
				let wxOId = newapp.globalData.wxOId
			
				uni.share({
					provider: "weixin", //分享服务提供商（即weixin|qq|sinaweibo）
					scene: 'WXSceneSession', //场景，可取值参考下面说明。
					type: 5, //分享形式
					title: shareText, //分享内容的标题
					// summary: '', //分享内容的摘要
					imageUrl: img, //图片地址
					miniProgram:{
						id: wxOId,
						path: 'homeSub/pages/registration/registration?memberId=' + memberId,
						type: 0,
						webUrl: newapp.globalData.imgUrl
					},
					success: function(res) {
						console.log("分享成功",res)
					},
					fail: function(err) {
						uni.showToast({
							title: '分享失败',
							icon: 'none',
							duration: 2000
						})
					}
				});
			},
		}
	};
</script>
<style lang="less">
	@import url("./inviteFriends.less");
</style>
